<!DOCTYPE html>
<html>

<head>
    <meta charset=" utf-8">
    <title>axios</title>
    <script src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.0/axios.js"></script>
</head>

<body>
    <button id="btn" onclick="test()">点击</button>
    <span id="txt"></span>
    <script>
        var setCookie = function (name, value, days) {
            if (days === void 0) { days = 300; }
            var exp = new Date();
            exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
            document.cookie = name + '=' + escape(value) + '; path=/;expires=' + exp.toUTCString();
        };

        setCookie('XSRF-TOKEN2', 'abcd');

        axios.interceptors.response.use(function (response) {
            console.log('---------');
            return response;
        }, function (error) {
            console.error('----', error);
        });

        axios.defaults.xsrfCookieName = 'XSRF-TOKEN2'; // default
        axios.defaults.xsrfHeaderName = 'X-XSRF-TOKEN2'; // default
        axios.defaults.validateStatus = function validateStatus(status) {
            return (status >= 200 && status < 300) || status === 304;
        };

        async function test() {
            $.get('/api', function (res) {
                console.log('-------jq-------', res);
            });

            let res = await axios.get('/api', {
                // `xsrfCookieName` 是用作 xsrf token 的值的cookie的名称
                // xsrfCookieName: 'XSRF-TOKEN2', // default
                // xsrfHeaderName: 'X-XSRF-TOKEN2', // default
                // validateStatus: function validateStatus(status) {
                //     return (status >= 200 && status < 300) ||  status === 304;
                // }
            });
            console.log(res);
            document.getElementById('txt').innerHTML = res.data;
        }
    </script>
</body>

</html>